﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RegisterationCommon;
using System.Data;
using RegisterationCommon.Common;

namespace RegisterationCommon.LeaveBoard
{
    /// <summary>
    /// 留言信息实体类
    /// </summary>
    public class Leave
    {
        #region 局部变量
        //留言ID
        private long _ID = 0;
        //学生姓名
        private string _StuName = "";
        //学生信息ID
        private int _StuID = 0;
        //留言标题
        private string _Title = "";
        //留言内容
        private string _Content = "";
        //所回复的主题ID  如果值为0，则表示属于留言主题
        private long _ReplySubjectID = 0;
        //接收对象 0=学生加长 1=教育局 2=学校
        private byte _LeaveSender = 0;
        //学校ID，如果LeaveSender指向学校，则此项需要输入学校ID，否则保持为0
        private int _SchID = 0;
        //留言时间
        private DateTime _LeaveTime = DateTime.Now;
        //回复时间
        private DateTime _ReplyTime = DateTime.Now;
        #endregion

        #region 属性列表
        /// <summary>
        /// 回复时间
        /// </summary>
        public DateTime ReplyTime
        {
            get { return _ReplyTime; }
            set { _ReplyTime = value; }
        }
        /// <summary>
        /// 留言时间
        /// </summary>
        public DateTime LeaveTime
        {
            get { return _LeaveTime; }
            set { _LeaveTime = value; }
        }
        /// <summary>
        /// 学校ID，如果LeaveSender指向学校，则此项需要输入学校ID，否则保持为0
        /// </summary>
        public int SchID
        {
            get { return _SchID; }
            set { _SchID = value; }
        }
        /// <summary>
        /// 接收对象 0=学生加长 1=教育局 2=学校
        /// </summary>
        public byte LeaveSender
        {
            get { return _LeaveSender; }
            set { _LeaveSender = value; }
        }
        /// <summary>
        /// 所回复的主题ID  如果值为0，则表示属于留言主题
        /// </summary>
        public long ReplySubjectID
        {
            get { return _ReplySubjectID; }
            set { _ReplySubjectID = value; }
        }
        /// <summary>
        /// 留言内容
        /// </summary>
        public string Content
        {
            get { return _Content; }
            set { _Content = value; }
        }
        /// <summary>
        /// 留言标题
        /// </summary>
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }
        /// <summary>
        /// 学生信息ID
        /// </summary>
        public int StuID
        {
            get { return _StuID; }
            set { _StuID = value; }
        }
        /// <summary>
        /// 学生姓名
        /// </summary>
        public string StuName
        {
            get { return _StuName; }
            set { _StuName = value; }
        }
        /// <summary>
        /// 留言ID
        /// </summary>
        public long ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        #endregion

        #region 方法列表
        #region 查询留言列表
        /// <summary>
        /// 分页获取留言(ReplySubjectID=0时获取主题  ReplySubjectID！=0时 获取主题下的留言信息)
        /// </summary>
        /// <param name="RP">数据集</param>
        /// <returns>返回JSON格式的字符串</returns>
        public string GetLeaveSubjectList(RParams RP)
        {
            //查询当前页
            DataTable dt = DBAction.ExecSP("sp_select_page", RP).Tables[0];
            //查询条件的总条数
            int totalCount = DBAction.GetDTFromSQL("select * from " + ETableName.Leave + " where " + RP.GetSqlParameter("Where").Value).Rows.Count;
            //返回Json
            return DBAction.DataTableToJson(dt, true, totalCount);
        }
        #endregion

        #region 查询ID查询留言信息
        /// <summary>
        /// 根据ID查询留言信息信息
        /// </summary>
        /// <param name="leaveID">留言编号</param>
        /// <returns>查询成功返回true,否则返回false</returns>
        public bool GetLeaveByID(long leaveID)
        {
            DataTable dt = DBAction.GetDTFromSQL("Select * From " + ETableName.Leave + " where id=" + leaveID);
            if (dt.Rows.Count < 1)
            {
                return false;
            }
            this._ID = leaveID;
            this._StuName = Convert.ToString(dt.Rows[0]["StuName"]);
            this._StuID = Convert.ToInt32(dt.Rows[0]["StuID"]);
            this._Title = Convert.ToString(dt.Rows[0]["Title"]);
            this._Content = Convert.ToString(dt.Rows[0]["Content"]);
            this._ReplySubjectID = Convert.ToInt64(dt.Rows[0]["ReplySubjectID"]);
            this._LeaveSender = Convert.ToByte(dt.Rows[0]["LeaveSender"]);
            this._SchID = Convert.ToInt32(dt.Rows[0]["SchID"]);
            this._LeaveTime = Convert.ToDateTime(dt.Rows[0]["LeaveTime"]);
            this._ReplyTime = Convert.ToDateTime(dt.Rows[0]["ReplyTime"]);
            return true;
        }
        #endregion

        #region 添加留言信息

        /// <summary>
        /// 添加留言信息
        /// </summary>
        /// <returns>添加成功返回true,否则返回false</returns>
        public bool AddLeave()
        {
            RParams RP = new RParams();
            RP.Add("StuName", this._StuName, SqlDbType.VarChar);
            RP.Add("StuID", this._StuID, SqlDbType.Int);
            RP.Add("Title", this._Title, SqlDbType.VarChar);
            RP.Add("Content", this._Content, SqlDbType.Text);
            RP.Add("ReplySubjectID", this._ReplySubjectID, SqlDbType.BigInt);
            RP.Add("LeaveSender", this._LeaveSender, SqlDbType.TinyInt);
            RP.Add("LeaveTime", this._LeaveTime, SqlDbType.DateTime);
            RP.Add("ReplyTime", this._ReplyTime, SqlDbType.DateTime);
            return AddLeave(RP);
        }
        /// <summary>
        /// 添加留言信息
        /// </summary>
        /// <param name="RP">数据集</param>
        /// <returns>添加成功返回true,否则返回false</returns>
        public bool AddLeave(RParams RP)
        {
            return DBAction.Insert(ETableName.Leave, RP);
        }

        #endregion

        #region 根据ID删除留言
        /// <summary>
        /// 根据ID删除留言
        /// </summary>
        /// <param name="leaeID">留言编号</param>
        /// <returns>删除成功返回true,否则返回false</returns>
        public bool DelLeaveByID(long leaeID)
        {
            RParams RP = new RParams();
            RP.Add("ID", leaeID, SqlDbType.BigInt);
            string whereFields = "ID=@ID";
            return DBAction.Delete(ETableName.Leave, whereFields, RP);
        }
        /// <summary>
        /// 根据ID删除留言
        /// </summary>
        /// <returns>删除成功返回true,否则返回false</returns>
        public bool DelLeaveByID()
        {
            return DelLeaveByID(this._ID);
        }
        #endregion

        #region 根据主题ID删除留言
        /// <summary>
        /// 根据主题ID删除留言
        /// </summary>
        /// <param name="leaeID">主题编号</param>
        /// <returns>删除成功返回true,否则返回false</returns>
        public bool DelLeaveBySubjectID(long subjectID)
        {
            RParams RP = new RParams();
            RP.Add("ReplySubjectID", subjectID, SqlDbType.BigInt);
            string whereFields = "ReplySubjectID=@ReplySubjectID or ID =@ReplySubjectID";
            return DBAction.Delete(ETableName.Leave, whereFields, RP);
        }
        /// <summary>
        /// 根据主题ID删除留言
        /// </summary>
        /// <returns>删除成功返回true,否则返回false</returns>
        public bool DelLeaveBySubjectID()
        {
            return DelLeaveBySubjectID(this._ReplySubjectID);
        }

        #endregion

        #region 修改留言信息

        /// <summary>
        /// 修改留言信息
        /// </summary>
        /// <returns>修改成功返回true,否则返回false</returns>
        public bool MdfLeave()
        {
            RParams RP = new RParams();
            RP.Add("ID", this._ID, SqlDbType.Int);
            RP.Add("StuName", this._StuName, SqlDbType.VarChar);
            RP.Add("StuID", this._StuID, SqlDbType.Int);
            RP.Add("Title", this._Title, SqlDbType.VarChar);
            RP.Add("Content", this._Content, SqlDbType.Text);
            RP.Add("ReplySubjectID", this._ReplySubjectID, SqlDbType.BigInt);
            RP.Add("LeaveSender", this._LeaveSender, SqlDbType.TinyInt);
            RP.Add("LeaveTime", this._LeaveTime, SqlDbType.DateTime);
            RP.Add("ReplyTime", this._ReplyTime, SqlDbType.DateTime);
            string colFields = "StuName,StuID,Title,Content,ReplySubjectID,LeaveSender,LeaveTime,ReplyTime";
            string whereFields = "ID=@ID";
            return MdfLeave(colFields, whereFields, RP);
        }

        /// <summary>
        /// 修改留言信息
        /// </summary>
        /// <param name="colFields">修改的列</param>
        /// <param name="whereFields">修改条件</param>
        /// <param name="RP">数据集</param>
        ///  <returns>修改成功返回true,否则返回false</returns>
        public bool MdfLeave(string colFields, string whereFields, RParams RP)
        {
            return DBAction.Update(ETableName.Leave, colFields, whereFields, RP);
        }
        #endregion

        #endregion
    }
}
